package com.jdj.framework.dao.mybatis.mapper;

import com.jdj.framework.dao.mybatis.entity.BaseEntity;
import com.jdj.framework.dao.mybatis.entity.condition.BaseEntityCondition;
import com.jdj.framework.dao.mybatis.provider.BaseMapperProvider;
import java.io.Serializable;
import java.util.List;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

/**
 * mapper的父类
 *
 * @author caijinbang
 * @date 2019-04-14 13:09
 */
public interface BaseMapper {
    /**
     * 查询记录总数
     *
     * @param condition
     * @return
     */
    <Condition extends BaseEntityCondition> int countByCondition(Condition condition);

    /**
     * 根据主键删除
     *
     * @param entityClass
     * @param primaryKey
     * @return
     */
    @DeleteProvider(type = BaseMapperProvider.class, method = "deleteByPrimaryKey")
    <Entity extends BaseEntity> int deleteByPrimaryKey(@Param("entityClass") Class<Entity> entityClass,
        @Param("primaryKey") Serializable primaryKey);

    /**
     * 插入有值字段
     *
     * @param entity
     * @return
     */
    @InsertProvider(type = BaseMapperProvider.class, method = "insertSelective")
    <Entity extends BaseEntity> int insertSelective(Entity entity);

    /**
     * 根据Condition条件批量查询
     *
     * @param condition
     * @return
     */
    <Entity extends BaseEntity, Condition extends BaseEntityCondition> List<Entity> selectByCondition(
        Condition condition);

    /**
     * 根据Condition条件批量查询（包含BLOB 数据）
     *
     * @param condition
     * @return
     */
    <Entity extends BaseEntity, Condition extends BaseEntityCondition> List<Entity> selectByConditionWithBLOBs(
        Condition condition);

    /**
     * 根据主键查询
     *
     * @param entityClass
     * @param primaryKey
     * @return
     */
    @SelectProvider(type = BaseMapperProvider.class, method = "selectByPrimaryKey")
    @ResultMap("BaseResultMap")
    <Entity extends BaseEntity> Entity selectByPrimaryKey(@Param("entityClass") Class<Entity> entityClass,
        @Param("primaryKey") Serializable primaryKey);

    /**
     * 根据主键修改有值字段
     *
     * @param entity
     * @return
     */
    @UpdateProvider(type = BaseMapperProvider.class, method = "updateByPrimaryKeySelective")
    <Entity extends BaseEntity> int updateByPrimaryKeySelective(Entity entity);

    /**
     * 根据主键修改(非空字符/非null)字段
     *
     * @param entity
     * @return
     */
    @UpdateProvider(type = BaseMapperProvider.class, method = "updateNotEmptyByPrimaryKey")
    <Entity extends BaseEntity> int updateNotEmptyByPrimaryKey(Entity entity);

    /**
     * 根据主键修改(非null)字段
     *
     * @param entity
     * @return
     */
    @UpdateProvider(type = BaseMapperProvider.class, method = "updateNotNullByPrimaryKey")
    <Entity extends BaseEntity> int updateNotNullByPrimaryKey(Entity entity);

    /**
     * 根据条件批量删除
     *
     * @param condition 条件
     * @return 影响行数
     */
    <Condition extends BaseEntityCondition> int deleteByCondition(Condition condition);

    /**
     * 根据条件批量更新
     *
     * @param entity    实体值
     * @param condition 条件
     * @return 影响行数
     */
    <Entity extends BaseEntity, Condition extends BaseEntityCondition> int updateByCondition(@Param("record") Entity entity, @Param("example") Condition condition);
}
